package app;

import conf.KeyVerifyConf;
import health.TestHealthCheck;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.apache.http.protocol.HTTP;
import res.ControlRes;
import res.TeacherRes;
import res.TokenRes;
import res.UserRes;
import util.HTTPTool;

import java.util.Arrays;

/**
 * Created by fin on 2017/7/7.
 */
public class KeyVerfiyApplication extends Application<KeyVerifyConf> {


    @Override
    public String getName() {
        return "keyverify";
    }

    @Override
    public void initialize(Bootstrap<KeyVerifyConf> bootstrap) {
        super.initialize(bootstrap);
        try {
            Class.forName("mybatis.conf.MybatisConf");
        } catch (ClassNotFoundException e) {
            System.out.println("mybatis加载失败");
        }

    }

    @Override
    public void run(KeyVerifyConf keyVerifyConf, Environment environment) throws Exception {


        //资源添加
        HTTPTool.name = keyVerifyConf.getName();
        HTTPTool.ip = keyVerifyConf.getIp();
        HTTPTool.port = keyVerifyConf.getPort();
        HTTPTool.url = keyVerifyConf.getUrl();

        //无数据库接口
        final UserRes userRes = new UserRes();
        final TokenRes tokenRes=new TokenRes();
        final TeacherRes teacherRes=new TeacherRes();
        final ControlRes controlRes = new ControlRes();

        //health check
        final TestHealthCheck healthCheck = new TestHealthCheck(keyVerifyConf.getName());


        //资源注册
        environment.healthChecks().register("keyverify",healthCheck);
        environment.jersey().register(userRes);
        environment.jersey().register(tokenRes);
        environment.jersey().register(teacherRes);
        environment.jersey().register(controlRes);
        HTTPTool.revokeAPI();
        HTTPTool.registerAPI();
    }

    public static void main(final String[] args) {
        Arrays.asList(
                //Application注册
                new KeyVerfiyApplication()
        ).forEach((l) -> {
            try {
                l.run(args);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
